package com.zhaohanlin.start;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @author zhaohanlin
 */

public class ListTest1 {


    @Test
    public void arrayList() {
        List<String> arrayList = new ArrayList<>(10);
//追加
        arrayList.add("china");
        arrayList.add("japan");
        arrayList.add("amlk");
//指定添加元素，其余元素依次向后位移
        arrayList.add(0, "korea");
        List<String> europe = new ArrayList<>();
        europe.add("中国");
        europe.add("蒙古");
//将europe序列体追加到arrayList中
        arrayList.addAll(europe);
        System.out.println("add over" + arrayList);
        System.out.println("0号元素" + arrayList.get(0));
        System.out.println("China首次出现的位置" + arrayList.indexOf("China"));
        System.out.println("UK最后出现的位置" + arrayList.lastIndexOf("UK"));
        System.out.println(arrayList.equals(europe) ? "等于europe序列" : "不等于europe序列");
        //for-i遍历
        for (int i = 0, j = arrayList.size(); i < j; i++) {
            System.out.println(i + ":" + arrayList.get(i));
        }
//for-e遍历
        for (String e : arrayList) {
            System.out.println("e:" + e);
//将一号位元素修改为UK

        }
        arrayList.set(1, "UK");
        System.out.println("将一号元素修改为" + arrayList);
        //截取序列0-3位元素，包括0位，不包括3位
        System.out.println("截取出0-3号片段" + arrayList.subList(0, 3));
//删除并返回1号元素
        System.out.println("删除一号位元素" + arrayList.remove(1));
        //删除指定元素
        System.out.println(arrayList.remove("amlk") ? "删除成功" : "删除失败");
        //删除europe序列
        arrayList.removeAll(europe);
        System.out.println("删除序列后" + arrayList);
//若序列中还有元素，则清空列表
        if (!arrayList.isEmpty()) {
            arrayList.clear();
            System.out.println("清空序列后" + arrayList);
        }
    }

    @Test
    public void linkedList() {
        LinkedList<String> linkedList = new LinkedList<>();
        linkedList.add("日本");
        linkedList.add("美国");
        linkedList.add("俄罗斯");
        linkedList.add("德国");
        linkedList.add("法国");
        linkedList.add(1, "泰国");
        linkedList.addFirst("中国");
        linkedList.addLast("美国");
        System.out.println("add over" + linkedList);
//获取元素
        System.out.println("0号位元素" + linkedList.get(0));
        System.out.println("链头头元素（不存在返回mull）" + linkedList.peekFirst());
        System.out.println("链头尾元素（不存在返回mull）" + linkedList.peekLast());
        System.out.println("链头头元素不存在返回值抛异常" + linkedList.getFirst());
        System.out.println("链头尾元素不存在返回值抛异常" + linkedList.getLast());
//for遍历
        for (String e : linkedList){
            System.out.println("e"+e);


        }
//修改
        linkedList.set(1,"UK");
        System.out.println("将表头一号位元素修改为"+linkedList);
//删除
        linkedList.remove(1);
linkedList.remove("UK");
for (String e:linkedList){
    System.out.println(e);

}linkedList.clear();
        System.out.println(linkedList);

    }
    @Test
    public void addList(){
     LinkedList<String>linkedList=new LinkedList<>();
     linkedList.add("美国");
     linkedList.add("日本");
     linkedList.add(0,"中国");
     for (String e:linkedList){
         System.out.println(e);
     }

    }
}
